package com.example.myproject.excel;
import com.spire.xls.*;

/**
 * 参考文档：https://zhidao.baidu.com/question/1754934471627276788.html
 *
 * 缺点：会产生授权警告
 */
public class MergeExcels {
    public static void main(String[] args){
        String[] inputFiles = new String[]{"d:/test/a.xlsx","d:/test/b.xlsx"};
        //创建一个新的Excel文档

        Workbook newBook = new Workbook();

        //清除默认的3张工作表

        newBook.getWorksheets().clear();


        //创建另一个Excel文档

        Workbook tempBook = new Workbook();


        //遍历数组，依次加载每个Excel文档并将文档中的所有工作表复制到新建的Excel文档中

        for (String file : inputFiles)
        {
            tempBook.loadFromFile(file);
            for (Object sheet: tempBook.getWorksheets())
            {
                newBook.getWorksheets().addCopy((Worksheet) sheet, WorksheetCopyType.CopyAll);
            }
        }
        //保存

        newBook.saveToFile("d:/test/MergeFiles.xlsx", ExcelVersion.Version2013);

    }



}
